package com.wentao.thisService.service.impl;

import com.wentao.thisCommon.model.RequestBase;
import com.wentao.thisCommon.model.ResultBase;
import com.wentao.thisFacade.business.AppCtrInterface;
import com.wentao.thisFacade.common.enums.ExceptionCode;
import com.wentao.thisFacade.common.enums.SuccessCode;
import com.wentao.thisFacade.common.exception.CustomException;
import com.wentao.thisFacade.domain.reqeust.VersionCheckRequest;
import com.wentao.thisFacade.domain.result.VersionCheckResult;
import com.wentao.thisService.mapper.VersionDao;
import com.wentao.thisService.model.VersionEntity;
import com.wentao.thisService.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 10:21
 * @Description: APP版本service实现
 */
@Service
public class VersionServiceImpl implements VersionService {

    @Value("${apkUpload.host.ip}")
    private String apkUploadIP;

    @Autowired
    private VersionDao versionDao;

    /**
     * @see VersionService#checkVersion(VersionCheckRequest)
     */
    @Override
    public VersionCheckResult checkVersion(VersionCheckRequest versionCheckRequest) throws Exception {
        Integer currentVersionCode = versionCheckRequest.getVersionCode();
        if (null == currentVersionCode) {
            throw new CustomException(ExceptionCode.NO_VERSIONCODE_ERROR);
        }

        // 最新版本信息
        VersionEntity searchVersion = new VersionEntity();
        List<VersionEntity> versionEntityList = versionDao.Search(searchVersion);
        if (null == versionEntityList || versionEntityList.size() == 0) {
            throw new CustomException(ExceptionCode.NO_VERSION_INFO_ERROR);
        }
        VersionEntity latestVersion = versionEntityList.get(0);
        Map<Integer, VersionEntity> versionCodeEntityMap = new HashMap<>();
        for (VersionEntity versionEntity : versionEntityList) {
            if (null != versionEntity) {
                versionCodeEntityMap.put(versionEntity.getVersionCode(), versionEntity);
            }
        }

        // 检测结果
        String updateStatus = "0";
        if (!currentVersionCode.equals(latestVersion.getVersionCode())) {
            if (latestVersion.getCompatibleMinCode() > currentVersionCode) {
                updateStatus = "2";
            } else if (latestVersion.getCompatibleMaxCode() < currentVersionCode) {
                updateStatus = "2";
            }
            if (latestVersion.getCompatibleMinCode() <= currentVersionCode && currentVersionCode <= latestVersion.getCompatibleMaxCode()) {
                updateStatus = "1";
            }
        }

        // 修改url，让结果能直接在网页打开
        String matchChar = "\\";
        String replaceChar = "/";
        if (latestVersion.getUrl().contains(matchChar)) {
            latestVersion.setUrl(String.format("%s%s", apkUploadIP, latestVersion.getUrl().replace(matchChar, replaceChar)));
        }

        VersionCheckResult versionCheckResult = new VersionCheckResult();
        versionCheckResult.setCurrentVersionCode(currentVersionCode);
        versionCheckResult.setCurrentVersionName(null == versionCodeEntityMap.get(currentVersionCode) ? "" : versionCodeEntityMap.get(currentVersionCode).getVersionName());
        versionCheckResult.setLatestVersionCode(latestVersion.getVersionCode());
        versionCheckResult.setLatestVersionName(latestVersion.getVersionName());
        versionCheckResult.setUpdateContent(latestVersion.getUpdateContent());
        versionCheckResult.setUpdateStatus(updateStatus);
        versionCheckResult.setUpdateURL(latestVersion.getUrl());

        return versionCheckResult;
    }
}
